package com.situ.ssm.user.controller;

import com.situ.ssm.user.pojo.User;
import com.situ.ssm.user.service.UserService;
import com.situ.ssm.util.ServletUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Project :springssm
 * @Author : yun&bo
 * @Date 2020/12/17 11:18
 * @Version
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public ModelAndView goIndex(ModelAndView modelAndView) {
        System.out.println("我最帅");
        modelAndView.setViewName("user/user_index");
        return modelAndView;
    }

    @GetMapping("/form")
    public ModelAndView goAddForm(ModelAndView modelAndView) {
        System.out.println("useradd");
        modelAndView.setViewName("user/user_add");
        return modelAndView;
    }

    /**
     * 上传用户头像
     *
     * @param multipartFile
     * @param request
     * @return
     */
    @PostMapping("/upload")
    public String doUpload(MultipartFile multipartFile, HttpServletRequest request) {
        String realPath = request.getServletContext().getRealPath("/");
        return userService.doUploadAvatar(multipartFile, realPath);
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @PostMapping
    public Integer doAaaUser(User user, HttpSession session){
        System.out.println(user);
        String createBy = ServletUtil.getUserCodeBySession(session);
        return  userService.doAddUSer(user,createBy);
    }

}
